package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgBrandMapper;
import com.ygqh.baby.po.YgBrand;
import com.ygqh.baby.po.YgBrandExample;
import com.ygqh.baby.service.YgBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class YgBrandServiceImpl implements YgBrandService {

    @Autowired
    private YgBrandMapper ygBrandMapper;

    @Override
    public List<YgBrand> find() {
        YgBrandExample example = new YgBrandExample();
        example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
        return ygBrandMapper.selectByExample(example);
    }

    @Override
    public int findCount() {
        YgBrandExample example = new YgBrandExample();
        example.createCriteria().andStatusNotEqualTo(DataStatus.Delete.name());
        return ygBrandMapper.countByExample(example);
    }

    @Override
    public YgBrand findById(Long id) {
        return ygBrandMapper.selectByPrimaryKey(id);
    }


    @Override
    public int save(YgBrand ygBrand) {
        return ygBrandMapper.insertSelective(ygBrand);
    }

    @Override
    public int update(YgBrand ygBrand) {
        return ygBrandMapper.updateByPrimaryKeySelective(ygBrand);
    }

    @Override
    public ResultSet<YgBrand> search(QueryInfo queryInfo, Long supplierId, String q) {
        int count = ygBrandMapper.countSuper(supplierId, q);
        if (count > 0) {
            List<YgBrand> list = ygBrandMapper.selectSuper(supplierId, queryInfo, q);

            ResultSet<YgBrand> resultSet = new ResultSet<YgBrand>(count, list);
            return resultSet;
        }
        return null;
    }

    @Override
    public int deleteSoft(Long[] ids) {
        Assert.notEmpty(ids);
        int count = 0;
        for (Long id : ids) {
            YgBrand ygBrand = this.findById(id);
            ygBrand.setStatus(DataStatus.Delete);
            count += ygBrandMapper.updateByPrimaryKeySelective(ygBrand);
        }

        return count;
    }

    @Override
    public int deleteSoft(Long id) {
        return this.deleteSoft(new Long[]{id});
    }

    @Override
    public int saveOrUpdate(YgBrand ygBrand) {
        int r = 0;
        if (ygBrand.getId() != null) {
            r = this.update(ygBrand);
        } else {
            r = this.save(ygBrand);
        }
        return r;
    }

    @Override
    public Boolean isSelfBrand(String brandName) {

        YgBrandExample example = new YgBrandExample();
        example.createCriteria().andBrandNameEqualTo(brandName);
        List<YgBrand> list = ygBrandMapper.selectByExample(example);
        if (list.size() > 0)
            return true;

        return false;
    }

    @Override
    public List<YgBrand> getBrandList(Long supplierId, QueryInfo queryInfo, String q) {
        return ygBrandMapper.selectSuper(supplierId, queryInfo, q);
    }

    @Override
    public List<YgBrand> findByIdList(List<Long> brandIdList) {
        YgBrandExample example = new YgBrandExample();
        example.createCriteria().andIdIn(brandIdList);
        return ygBrandMapper.selectByExample(example);
    }


}

